<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">

    <div class="layui-form-item">
        <div class="layui-input-block">
            <input autocomplete="off" class="layui-input"  id="id" type="hidden"
                   name="id"
                   placeholder="id" style="background:#dedede">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">权限名称</label>
        <div class="layui-input-block">
            <input autocomplete="off" class="layui-input"  id="title"
                   name="title"
                   placeholder="权限名称" style="background:#dedede" type="text">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">父级菜单</label>
        <div class="layui-input-block">
            <select id="parentMenu" name="pid">
                <option value="">全部</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">菜单url</label>
        <div class="layui-input-block">
            <input autocomplete="off" class="layui-input" id="menuUrl"
                   name="href" placeholder="菜单url" type="text">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-block">
            <select id="parentNode" name="sort" >
                <option value="">全部</option>
                <option value="0">模块</option>
                <option value="1">导航</option>
                <option value="2">功能</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">图标</label>
        <div class="layui-input-block">
            <input autocomplete="off" class="layui-input" id="menuIcon"
                   name="icon" placeholder="图标" type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input checked="checked" id="status" lay-filter="switchTest1" lay-skin="switch" lay-text="启用|停用"
                   name="status" type="checkbox">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">跳转方式</label>
        <div class="layui-input-block">
            <select id="parentNode" name="target" >
                <option value="">全部</option>
                <option value="_self">本页面</option>
                <option value="_blank">新页面</option>
            </select>
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script th:src="@{/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {

            if ($("#status").is(':checked')) {
                data.field.status = '1';
            } else {
                data.field.status = '0';
            }
            var datas = data.field;

            var index = layer.alert("确认提交？", {
                title: '提示'
            }, function () {
                $.ajax({
                    url: '/menu/addMenu',
                    type: "POST",
                    data: datas,
                    success: function (data) {
                        if (data.code == 200) {
                            layer.alert(data.message,{
                                title: "提示",
                            },function (){
                                // 关闭弹出层
                                layer.close(index);
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                                window.parent.parent.location.reload();
                            })
                        }else{
                            layer.alert(data.message,{
                                title: "提示",
                            },function (){
                                // 关闭弹出层
                                layer.close(index);
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                                window.parent.location.reload();
                            })
                        }
                    }
                });

            });
            return false;
        });

        /*$.ajax({
            //动态获取下拉选项的接口，返回数据主要是id+name 需要同步方便回写
            url: '/menu/queryType',
            dataType: 'json',
            type: 'get',
            async: false,
            success: function (data) {
                let str="<option value=''>全部</option>";
                for(let menus of data){
                    //组装数据
                    str+=`<option value='${menus.sort}'>${menus.node}</option>`;
                }
                //jquery赋值方式
                $("#parentNode").html(str);
                //重新渲染生效
                form.render();
            }
        });*/

        $.ajax({
            //动态获取下拉选项的接口，返回数据主要是id+name 需要同步方便回写
            url: '/menu/queryNode',
            dataType: 'json',
            type: 'get',
            async: false,
            success: function (data) {
                let str="<option value=''>全部</option>";
                for(let menus of data){
                    //组装数据
                    str+=`<option value='${menus.id}'>${menus.title}</option>`;
                }
                //jquery赋值方式
                $("#parentMenu").html(str);
                //重新渲染生效
                form.render();
            }
        });

    });
</script>
</body>
</html>